package yxy.game.pm2.active.command.sdk.shengye;

import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.sdk.shengye.ShengYeSubscribeModule;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.SdkActiveMessage;
import yxy.game.pm2.message.SdkActiveMessage.ShengYeSubscribeRequest;

import java.util.Map;

@MessageType(proto = Command.ShengYeSubscribe, classtype = SdkActiveMessage.ShengYeSubscribeRequest.class)
public class CMD_ShengYeSubscribe extends Command<ShengYeSubscribeRequest> {

		private static final Logger logger = Logger.create(CMD_ShengYeSubscribe.class);
		
		@Override
		public void execute(MessageSource messageSource, ShengYeSubscribeRequest request) throws Exception {
			String playercode = messageSource.getTag();
			String template = request.getTemplate();
			int type = request.getType();
			SdkActiveMessage.ShengYeSubscribeResponse.Builder response = SdkActiveMessage.ShengYeSubscribeResponse.newBuilder();
			
			Map<String, Long> map = ShengYeSubscribeModule.all();
			try {
				if(ShengYeSubscribeModule.lockShengYeSubscribe()) {
					long nexttimes = ShengYeSubscribeModule.nexttimes(playercode);
					ShengYeSubscribeModule.touch().put(playercode, nexttimes);
				}
			}catch (Exception e) {
				e.printStackTrace();
			} finally {
				ShengYeSubscribeModule.unlockShengYeSubscribe();
			}
					
			send(messageSource, response, Result.Success);
		}

		private void send(MessageSource messageSource, SdkActiveMessage.ShengYeSubscribeResponse.Builder response, Result result) {
			response.setResult(result.packResult());
			MessageEmitterHandler.getInstance().send(messageSource, response.build());
		}
		
	}

